const { contextBridge, ipcRenderer } = require('electron');

// 向渲染进程暴露安全的API
contextBridge.exposeInMainWorld('electronAPI', {
  // 读取文件
  readFile: (filePath) => {
    return new Promise((resolve, reject) => {
      ipcRenderer.send('read-file', filePath);
      ipcRenderer.once('file-content', (event, result) => {
        if (result.success) {
          resolve(result.content);
        } else {
          reject(new Error(result.error));
        }
      });
    });
  },

  // 显示对话框
  showDialog: (options) => {
    return new Promise((resolve) => {
      ipcRenderer.send('show-dialog', options);
      ipcRenderer.once('dialog-result', (event, result) => {
        resolve(result);
      });
    });
  },

  // 应用版本信息
  appVersion: () => {
    return process.env.npm_package_version || '1.0.0';
  }
});

// 监听主进程消息示例
ipcRenderer.on('app-update', (event, data) => {
  console.log('应用更新信息:', data);
});